/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.apt.matrimony.methods;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apt.matrimony.db.ConvertDateTime;
import org.apt.matrimony.db.MyConnection;
import org.apt.matrimony.utils.Tools;

/**
 *
 * @author yoko
 */
public class Inserts {
  public static boolean sendRequest(String fromUser,String text,int toUser){
       Connection cnn=null;
        PreparedStatement stm=null;
        int fromUserId=-1;
     try {
//         fromUserId=selects.getAccountIdByAccountName(fromUser);
         Calendar cal=Calendar.getInstance();
         Date time=cal.getTime();
         String sTime=Tools.convertTime2String(time);
         String sql="Insert into Requests(Request_Text,Request_Sender_Id,Request_Time,Account_Id) values(?,?,?,?)";
         cnn=MyConnection.openConnection();
         stm=cnn.prepareStatement(sql);
         stm.setString(1, text);
         stm.setInt(2, toUser);
         stm.setString(3, sTime);
         stm.setInt(4, fromUserId);
         int status=stm.executeUpdate();
         if(status==1){
             return true;
         }

     } catch (Exception e) {
         System.out.println(e.getMessage());
     } finally{
         MyConnection.closeConnection(cnn, stm, null);
     }
     return false;
  }
public static boolean insertImage(String fromUser,String imageType,String  imagePath){
       Connection cnn=null;
        PreparedStatement stm=null;
        int fromUserId=-1;
        int imageTypeId=-1;
     try {
//         fromUserId=selects.getAccountIdByAccountName(fromUser);
//         imageTypeId=selects.getImageTypeIdByTypeName(imageType);
         Calendar cal=Calendar.getInstance();
         Date time=cal.getTime();
         String sTime=Tools.convertTime2String(time);
         String sql="Insert into Images(Account_Details_Id,Image_Type_Id,Image_Path) values(?,?,?)";
         cnn=MyConnection.openConnection();
         stm=cnn.prepareStatement(sql);
         stm.setInt(1, fromUserId);
         stm.setInt(2, imageTypeId);
         stm.setString(3, imagePath);
         stm.setInt(4, fromUserId);
         int status=stm.executeUpdate();
         if(status==1){
             return true;
         }

     } catch (Exception e) {
         System.out.println(e.getMessage());
     } finally{
         MyConnection.closeConnection(cnn, stm, null);
     }
     return false;
  }

  /*----------Hanh Ngo Session-----------------*/
   public static int insertBasicAccount(String username, String pass, String email, String payName) {

        int insert = 0;
        Connection cnn = null;
        PreparedStatement pst = null;
        String tblName = "Accounts";
        String[] columnName = {"User_Password", "User_Name", "User_Email", "Account_Status", "Account_Role","Date_Registed", "Date_Expired"};
        String dateReg = ConvertDateTime.getRegisterDate();
        String dateEx = ConvertDateTime.addDate(payName, dateReg);
        String sql = sqlInsertComm(tblName, columnName);

        try {
            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);

            pst.setString(1, pass);
            pst.setString(2, username);
            pst.setString(3, email);
            pst.setBoolean(4, true);
            pst.setString(5, "user");
            pst.setString(6, dateReg);
            pst.setString(7, dateEx);
            
            insert = pst.executeUpdate();
            if (insert > 0) {
                System.out.println("Thanh cong");
            } else {

                System.out.println("That bai");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return insert;
    }

    
    public static int insertDetailAccount(String username, String Full_Name, String Gender, String Date_Of_Birth, String Marital_Status, String Mother_Tongue, float Height, int Contact_Number, String Address, int cityID) {


        int insert = 0;
        Connection cnn = null;
        PreparedStatement pst = null;
        String tblName = "Account_Details";
        String[] columnName = {"Account_Id", "Full_Name", "Gender", "Date_Of_Birth", "Marital_Status", "Mother_Tongue", "Height", "Contact_Number", "Caste", "Address", "City_Id", "Coutry_Id", "Family_Details"};
        //lấy ra AccountID: 

        int account_Id = Updates.getAccountID(username);

        String sql = sqlInsertComm(tblName, columnName);

        try {
            //String date_of_birth = ConvertDateTime.dateConvertToString(Date_Of_Birth);

            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);


            pst.setInt(1, account_Id);
            pst.setString(2, Full_Name);
            pst.setString(3, Gender);
            pst.setString(4, Date_Of_Birth);
            pst.setString(5, Marital_Status);
            pst.setString(6, Mother_Tongue);
            pst.setFloat(7, Height);
            pst.setInt(8, Contact_Number);
            pst.setString(9, "");
            pst.setString(10, Address);
            pst.setInt(11, cityID);
           
            pst.setString(13, "");

            insert = pst.executeUpdate();
            if (insert > 0) {
                System.out.println("Thanh cong");
            } else {

                System.out.println("That bai");
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return insert;
    }

    public static int insertDate_Expired(String username, String pay) {

        // Update tai khoan da co-- them ngày đăng kí + ngày hết hạn

        int update = 0;
        Connection cnn = null;
        PreparedStatement pst = null;
        String tblName = "Accounts";
        String[] columnName = {"Date_Registed", "Date_Expired"};


        String date = ConvertDateTime.getRegisterDate();

        String dateEx = ConvertDateTime.addDate(pay, date);

        String sql = sqlInsertComm(tblName, columnName);

        try {
            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);
            pst.setString(1, date);
            pst.setString(2, dateEx);
            update = pst.executeUpdate();
            if (update > 0) {
                System.out.println("Thanh cong");
            } else {

                System.out.println("That bai");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return update;
    }

    public static int insertCountries(int status, String countryName) {
        int insert = 0;
        Connection cnn = null;
        PreparedStatement pst = null;


        String tblName = "Countries";
        String[] columnName = {"Country_Status", "Country_Name"};

        String sql = sqlInsertComm(tblName, columnName);


        try {
            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);

            pst.setInt(1, status);
            pst.setString(2, countryName);

            insert = pst.executeUpdate();
            if (insert > 0) {
                System.out.println("Thanh cong");
            } else {

                System.out.println("That bai");
            }


        } catch (SQLException ex) {
            Logger.getLogger(Inserts.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return insert;

    }

    
  
    // Insert bang Payment Detail
    public static int insertPaymentDetail(String payTypeID, String username) {
        int insert = 0;
        Connection cnn = null;
        PreparedStatement pst = null;


        String tblName = "Payment_Details";
        String[] columnName = {"Payment_Type_Id", "Account_Id"};

        String sql = sqlInsertComm(tblName, columnName);
        int payType = Integer.parseInt(payTypeID);
        int accountID = Updates.getAccountID(username);

        try {
            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);
            pst.setInt(1, payType);
            pst.setInt(2, accountID);

            insert = pst.executeUpdate();
            if (insert > 0) {
                System.out.println("Pay Thanh cong");
            } else {

                System.out.println("Pay That bai");
            }


        } catch (SQLException ex) {
            Logger.getLogger(Inserts.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return insert;

    }

    public static String sqlInsertComm(String tblName, String[] columnName) {

        String sql = "Insert into " + tblName + " (";
        if (columnName != null) {
            for (int i = 0; i < columnName.length - 1; i++) {
                sql += " " + columnName[i] + " , ";
            }
            sql += columnName[columnName.length - 1] + ") values(";

            for (int i = 0; i < columnName.length - 1; i++) {
                sql += " ?,";
            }
            sql += "? )";
        }
        System.out.println("SQL - Insert " + tblName + ": " + sql);

        return sql;
    }

    public static String sqlSelectComm(String tblName) {
        String sql = "SELECT * FROM " + tblName;
        return sql;

    }

    public static void main(String[] args) {

        int ins = insertPaymentDetail("1", "Hanh Hanh");
        System.out.println(ins);


    }
}
